﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class LineDebug : System.Web.UI.Page
    {
        public PJLineDebug result = null;
        private PJLineDebugManager objPJLineDebugManager = new PJLineDebugManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        public int workParentId;
        public int fromPage;
        public int pjCount;
        public int statusNo;
        //public string serialNum;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
                fromPage = Convert.ToInt32(Request.QueryString["FromPage"]);
                try
                {
                    pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 18) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 7, 18) + objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 8, 18);
                    statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
                    result = objPJLineDebugManager.GetLineDebug(workParentId);
                    if (result != null)
                    {
                        txtSerialNum.Text = result.SerialNum;
                        ddlOrderType.SelectedItem.Text = result.OrderType;
                        txtLineNum.Text = result.LineNum;
                        txtOrderStartSerialNum.Text = result.OrderStartSerialNum;
                        txtSapAsset.Text = result.SapAsset;
                        txtSapBill.Text = result.SapBill;
                        txtData.Text = result.Statues;
                        if (fromPage == 0 || statusNo != 100)
                        {
                            if (pjCount > 0 && statusNo < 1) //项目
                            {
                                txtLineNum.Enabled = true;
                                Update1.Visible = true;
                            }
                            if (pjCount > 0 && statusNo == 2)//财务
                            {
                                txtSapBill.Enabled = true;
                                Update3.Visible = true;
                            }
                            if (pjCount > 0 && statusNo == 3) //项目仓库
                            {
                                txtSapAsset.Enabled = true;
                                Update2.Visible = true;
                            }
                           
                        }
                        if (statusNo == 100)
                        {
                            Update3.Visible = true;
                        }
                    }
                    else
                    {
                        if (statusNo == 0)
                        {
                            EnabledControl1();
                            Create.Visible = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
            
        }

        protected void Create_Click(object sender, EventArgs e)
        {
            try
            {
                PJLineDebug objPJLineDebug = new PJLineDebug()
                {
                    WorkParentId = workParentId,
                    SerialNum = ddlOrderType.SelectedValue + txtOrderStartSerialNum.Text.Substring(6, 2),
                    OrderType = ddlOrderType.SelectedItem.Text,
                    LineNum = txtLineNum.Text.ToString().Trim(),
                    OrderStartSerialNum = txtOrderStartSerialNum.Text.ToString().Trim()
                };
                string createResult = objPJLineDebugManager.InsertIsNoSucess(objPJLineDebug);
                if (createResult != "false")
                {
                    string msg = string.Format("<script>alert('流水号:{0}保存成功！')</script>", createResult);
                    string str = string.Format("<script>window.close();</script>");
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
                }
                else
                {
                    string msg = string.Format("<script>alert('保存失败！')</script>", result);
                    Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                }
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Update1_Click(object sender, EventArgs e)//项目更改
        {
            PJLineDebug objPJLineDebug = new PJLineDebug()
            {
                WorkParentId = workParentId,
                LineNum = txtLineNum.Text.ToString()
            };
            try
            {
                objPJLineDebugManager.UpdateInfo(objPJLineDebug);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);

            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Update2_Click(object sender, EventArgs e)
        {
            try
            {
                objPJLineDebugManager.UpdateSapAsset(txtSapAsset.Text.ToString().Trim(), workParentId);
                objPJLineDebugManager.UpdateStatues(workParentId);
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void Update3_Click(object sender, EventArgs e)
        {
            try
            {
                objPJLineDebugManager.UpdateSapBill(txtSapBill.Text.ToString().Trim(), workParentId);
                
                string msg = string.Format("<script>alert('保存成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        void EnabledControl1()
        {
            ddlOrderType.Enabled = true;
            txtOrderStartSerialNum.Enabled = true;
            txtLineNum.Enabled = true;
        }
    }
}